package leaf.science.pages
{
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.utils.setTimeout;
	
	import leaf.science.Global;
	import leaf.science.ui.camera.Camera;
	import leaf.science.ui.featuresflayer.FeaturesPlayer;

	public class PageFeatures extends JogPageBase
	{
		public var featuresPlayer:FeaturesPlayer;
		
		public function PageFeatures()
		{
			super();
			init();
		}
		
		override protected function onRightSelect (evt:Event):void
		{
			featuresPlayer.data = _data..feature[rightMenu.selectedIndex];
			updateFeature();
			setTimeout(featuresPlayer.open, 400);
		}
		
		override protected function onKeyUp(evt:KeyboardEvent):void
		{
			if (!featuresPlayer.isOpen) rightMenu.onKeyUp(evt);
		}
		
		override protected function registerCameraType():void
		{
			camera = MovieClip( _loader.content ) as Camera;
		}
		
		private function onNav(evt:Event):void
		{
			switch (evt.type){
				case 'next':
				rightMenu.selectedIndex--;
				break;
				
				case 'prev':
				rightMenu.selectedIndex++;
				break;
			}
			updateFeature();
		}
		
		private function updateFeature():void
		{
			featuresPlayer.data = _data..feature[rightMenu.selectedIndex];
			featuresPlayer.content.next_mc.visible = rightMenu.selectedIndex != 0;
			featuresPlayer.content.prev_mc.visible = rightMenu.selectedIndex != _data..feature.length() - 1;
		}
		
		
		override protected function init ():void
		{
			_message = "Choose a Feature";
			_camSwfURL = "../swf/FeaturesCamera.swf";
			_data = Global.DATA_FEATURES;
			
			_menuItems = [];
			var count:uint = 0;
			for each (var item:XML in _data..feature){
				_menuItems.push({id:count, title:item.title.toString()});
				count++;
			}
			
			super.init();
			
			featuresPlayer = new FeaturesPlayer();
			featuresPlayer.addEventListener('next', onNav);
			featuresPlayer.addEventListener('prev', onNav);
			addChild(featuresPlayer);
			
		}
				
		override public function toString():String
		{
			return 'features';
		}
		
	}
}